//
//  HomePageViewController.m
//  GYWBaseAPP
//
//  Created by 郭艺伟 on 16/5/11.
//  Copyright © 2016年 郭艺伟. All rights reserved.
//

#import "HomePageViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <Masonry/Masonry.h>
#import "GradeTableViewCell.h"
#import "GradeTableViewHeaderView.h"
#import "AMNoticeCardTableViewCell.h"
#import "AMNoticeCard.h"
#import <UITableView+FDTemplateLayoutCell/UITableView+FDTemplateLayoutCell.h>


#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define RandomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]
@interface HomePageViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *tableViewArr;
@property (nonatomic, strong) NSMutableArray *tableViewDataArr;

@property (nonatomic, strong) UIView *headView;
@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    AFNetworkReachabilityManager *reachability = [AFNetworkReachabilityManager sharedManager];
    [reachability setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: {
                NSLog(@"AFNetworkReachabilityStatusUnknown");
                break;
            }
            case AFNetworkReachabilityStatusNotReachable: {
                NSLog(@"AFNetworkReachabilityStatusNotReachable");
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN: {
                NSLog(@"AFNetworkReachabilityStatusReachableViaWWAN");
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi: {
                NSLog(@"AFNetworkReachabilityStatusReachableViaWiFi");
                break;
            }
        }
    }];
    [reachability startMonitoring];
    
    
    /*
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 80, 0));
    }];
    
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    for (NSInteger i = 0; i < 3; i++) {
        UITableView *tableView = [[UITableView alloc] init ];//WithFrame:CGRectZero style:UITableViewStyleGrouped];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (i % 2) {
            tableView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
        }else{
            tableView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];
        }
        tableView.backgroundColor = UIColorFromRGB(0xf6f6f6);
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.bounces = NO;
        tableView.tag = i;
        //tableView.tableHeaderView = self.headView;
        [self.scrollView addSubview:tableView];
        [self.tableViewArr addObject:tableView];
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.scrollView).offset(width * i);
            make.top.equalTo(self.scrollView).offset(0);
            make.width.equalTo(self.scrollView);
            make.height.equalTo(self.scrollView.mas_height);
//            make.bottom.equalTo(self.scrollView);
            
        }];
    }
    
    self.scrollView.contentSize = CGSizeMake(width*3, 0);
     */
    
    UITableView *tableView = [[UITableView alloc] init ];//WithFrame:CGRectZero style:UITableViewStyleGrouped];
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = UIColorFromRGB(0xf6f6f6);
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.bounces = NO;
    //tableView.tableHeaderView = self.headView;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(0);
        make.right.equalTo(self.view);
        make.height.equalTo(self.view.mas_height);
    }];
    
    [tableView registerClass:[AMNoticeCardTableViewCell class] forCellReuseIdentifier:@"AMNoticeCardTableViewCell"];
    AMNoticeCard *noticeCard = [[AMNoticeCard alloc] init];
    noticeCard.typeTitle = @"通知公告";
    noticeCard.title = @"关于99u无法登陆的紧急公告";
    noticeCard.desContent = @"自ND新LOGO发布以来，公司品牌视觉形象全面焕新，为配合各部门不同场合的使用需求，增强新LOGO实际应用与宣品延展的效果，现新增横式组合的ND新LOGO，同学们如有需要可以自行下载使用。http://777.nd.com.cn/news/news.aspx?id=20051";
    noticeCard.time = @"5月25号 17:30";
    noticeCard.isTop = YES;
    noticeCard.isShowVoteBtn = YES;
    noticeCard.voteCount = 88;
    noticeCard.isShowAnnexBtn = NO;
    noticeCard.isShowCommentBtn = YES;
    noticeCard.commentCount = 77;
    
    AMNoticeCard *noticeCard1 = [[AMNoticeCard alloc] init];
    noticeCard1.typeTitle = @"通知公告";
    noticeCard1.title = @"塑高效团队，赢万元大奖";
    noticeCard1.desContent = @"当当~本年度ND百团大战之“高效行动”已拉开序幕";
    noticeCard1.time = @"5月25号 16:30";
    noticeCard1.isTop = NO;
    noticeCard1.isShowVoteBtn = YES;
    noticeCard1.voteCount = 100;
    noticeCard1.isShowAnnexBtn = YES;
    noticeCard1.annexCount = 99;
    
    [self.tableViewDataArr addObjectsFromArray:@[noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1,noticeCard,noticeCard1]];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //内容类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
    
    NSMutableDictionary *parameters = @{@"login_name":@"test23232321",
                                        @"password":@"e10adc3949ba59abbe56e057f20f883e",
                                        @"app_id":@"happiness",
                                        @"role":@"member",
                                        @"real_name":@"真实姓名",
                                        @"sign":@"签名",
                                        @"portrait":@"",
                                        @"phone":@"18088888888",
                                        @"gender":@(1)
                                        }.mutableCopy;
    //post请求
    [manager POST:@"http://120.76.200.15:8888/uc/user/register" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        //数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //数据请求成功后，返回 responseObject 结果集
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //数据请求失败，返回错误信息原因 error
        NSInteger errorCode = [error code];
        NSError *customerError = nil;
        switch (errorCode) {
            case -1011:
            {
                NSData *errorData = [error.userInfo objectForKey:@"com.alamofire.serialization.response.error.data"];
                NSDictionary *errDict =[NSJSONSerialization JSONObjectWithData:errorData options:0 error:nil];
                if ([errDict objectForKey:@"message"]) {
                    customerError = [[NSError alloc] initWithDomain:errDict[@"message"] code:[error code]userInfo:nil];
                }else{
                    customerError = [[NSError alloc] initWithDomain:error.localizedDescription code:[error code] userInfo:nil];
                }
                break;
            }
                
            default:
                customerError = [[NSError alloc] initWithDomain:error.localizedDescription code:[error code] userInfo:nil];
                break;
        }
        
        NSLog(@"%@",[customerError domain]);
//        NSData *data = [error.userInfo objectForKey:@"com.alamofire.serialization.response.error.data"];
//        NSString *errorStr =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//        NSLog(@"%@",error);
//        NSLog(@"%@",error.domain);
    }];
    
    
}

- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [UIColor grayColor];
        _scrollView.pagingEnabled = YES;
    }
    return _scrollView;
}

-(NSMutableArray *)tableViewArr
{
    if (_tableViewArr == nil) {
        _tableViewArr = [NSMutableArray array];
    }
    return _tableViewArr;
}


-(NSMutableArray *)tableViewDataArr
{
    if (_tableViewDataArr == nil) {
        _tableViewDataArr = [NSMutableArray array];
    }
    return _tableViewDataArr;
}

-(UIView *)headView
{
    if (_headView == nil) {
        _headView = [[UIView alloc] init];
        _headView.backgroundColor = UIColorFromRGB(0xf5f6f6);
        _headView.backgroundColor = [UIColor redColor];
        _headView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200);
    }
    return _headView;
}

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableViewDataArr.count;
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*GradeTableViewCell *cell = [GradeTableViewCell cellWithTableView:tableView];
    cell.grage = GrageType_S;
    return cell;
     */
    AMNoticeCardTableViewCell *cell = [AMNoticeCardTableViewCell cellWithTableView:tableView];
    cell.noticeCard = self.tableViewDataArr[indexPath.row];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:@"AMNoticeCardTableViewCell" configuration:^(AMNoticeCardTableViewCell *cell) {
        cell.fd_enforceFrameLayout = NO;
        cell.noticeCard = self.tableViewDataArr[indexPath.row];
    }];
//    return 190;
//    return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

//-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 100;
//}
//
//
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    GradeTableViewHeaderView *sectionHeaderView = [GradeTableViewHeaderView headerViewWithTableView:tableView];
//    switch (tableView.tag) {
//        case 0:
//        {
//            GradeHeader *header = [[GradeHeader alloc] init];
//            header.mainTitle = @"第一项考评,招聘渠道维护及拓展";
//            header.subTitle = @"第一项考评副标题，二哥哥哥哥更二个老公家二楼见了就饿了就了几个了就挂了就挂了就挂了饥饿刚接了个久了就过了就够了两个结论个";
//            header.reference = @"参考数据:当前到岗人数3人";
//            header.standard = @"考评数据:DFDFDFDFDFDFDFD";
//            sectionHeaderView.header = header;
//            
//        }
//            break;
//        case 1:
//        {
//            GradeHeader *header = [[GradeHeader alloc] init];
//            header.mainTitle = @"第一项考评,招聘渠道维护及拓展";
//            header.reference = @"参考数据:当前到岗人数3人";
//            header.standard = @"考评数据:DFDFDFDFDFDFDFD";
//            sectionHeaderView.header = header;
//            
//        }
//            break;
//        case 2:
//        {
//            
//            GradeHeader *header = [[GradeHeader alloc] init];
//            header.mainTitle = @"第一项考评,招聘渠道维护及拓展";
//            header.reference = @"参考数据:当前到岗人数3人";
//            sectionHeaderView.header = header;
//        }
//            break;
//            
//        default:
//            sectionHeaderView.header = nil;
//            break;
//    }
//    return sectionHeaderView;
//}

@end
